/**
 * Created by ChenYong on 2017-04-12.
 *
 * 通知列表。
 */

$(function () {
    /** 创建人的 jQuery 对象 */
    var $noticeCreatePerson = $("#notice_create_person");
    /** 通知标题的 jQuery 对象 */
    var $noticeName = $("#notice_name");
    /** 通知级别的 jQuery 对象 */
    var $noticeLevel = $("#notice_level");
    /** 开始日期的 jQuery 对象 */
    var $startDate = $("#start_date");
    /** 结束日期的 jQuery 对象 */
    var $endDate = $("#end_date");
    /** 清空开始日期的 jQuery 对象 */
    var $startDateCloseBtn = $("#start_date_close_btn");
    /** 清空结束日期的 jQuery 对象 */
    var $endDateCloseBtn = $("#end_date_close_btn");
    /** 搜索按钮的 jQuery 对象 */
    var $searchNoticeBtn = $("#search_notice_btn");
    /** 通知模态框的 jQuery 对象 */
    var $noticeModal = $("#notice_modal");
    /** 通知列表的表格，DataTables 的 API 对象 */
    var noticeTable = null;

    // 选择开始日期
    $startDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 选择结束日期
    $endDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 清空开始日期
    $startDateCloseBtn.click(function () {
        $startDate.val("");
    });

    // 清空结束日期
    $endDateCloseBtn.click(function () {
        $endDate.val("");
    });

    // 监听通知级别下拉列表搜索
    $noticeLevel.change(function () {
        searchData();
    });

    // 点击搜索按钮
    $searchNoticeBtn.click(function (e) {
        searchData();
    });

    // 监听通知模态框打开
    $noticeModal.on("shown.bs.modal", function() {
        // 将滚动条滚动到顶部
        $noticeModal.find(".modal-body").scrollTop("0px");
    });

    // 监听通知模态框关闭
    $noticeModal.on("hidden.bs.modal", function() {
        $noticeModal.find(".modal-body").html("");
    });

    // 点击查看通知
    $(document).on("click", ".view_notice", function () {
        var $this = $(this);
        var id = $this.data("id");

        $.myAjax({
            url : "web/notice/detail.html",
            modal : true,
            dataType : "html",
            showHtml : false,
            data : {
                id : $this.data("id")
            },
            successFn : function(data) {
                $noticeModal.find(".modal-body").html(data);

                $noticeModal.modal("show");
            }
        });
    });

    // 展示通知表格
    noticeTable = $("#notice_table").myDataTable({
        url: "web/notice/list.json",
        searchBtn: $searchNoticeBtn,
        sScrollY: "450px",
        sScrollX: true,
        sScrollXInner: "100%",
        aaSorting: [[4, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "checkbox",
            sTitle: "<label class='pos-rel' title='全选'><input type='checkbox' class='ace'><span class='lbl'></span></label>",
            sWidth: "50px",
            sClass: "center",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html("<label class='pos-rel'><input type='checkbox' class='ace' data-id='{0}'><span class='lbl'></span></label>".format(rowData.id));
            }
        }, {
            mData: "noticeName",
            sName: "noticeName",
            sTitle: "通知标题"
        }, {
            mData: "noticeCreatePerson",
            sName: "noticeCreatePerson",
            sTitle: "创建人",
            sWidth: "200px"
        }, {
            mData: "noticeLevel",
            sName: "noticeLevel",
            sTitle: "通知级别",
            sWidth: "200px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                if (rowData.noticeLevel == "1") {
                    $cell.html("国家（省）");
                } else if (rowData.noticeLevel == "2") {
                    $cell.html("贵阳市");
                } else if (rowData.noticeLevel == "3") {
                    $cell.html("清镇市");
                } else {
                    $cell.html(rowData.noticeLevel);
                }
            }
        }, {
            mData: "noticeCreateTime",
            sName: "noticeCreateTime",
            sTitle: "<i class='ace-icon iconfont icon-time bigger-110'></i>时间",
            sWidth: "200px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html(rowData.noticeCreateTime.substring(0, 10));
            }
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "100px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                // 单元格的 jQuery 对象
                var $cell = $(cell);

                $cell.find("div.action-buttons").append("<a class='c-blue view_notice' title='查看' data-id='{0}' href='javascript:void(0);'><i class='ace-icon iconfont icon-eye bigger-200'></i></a>".format(rowData.id));
            }
        }]
    });

    /**
     *
     * 搜索数据。
     *
     */
    function searchData() {
        // 创建人
        if ($.isNotBlank($noticeCreatePerson.val()) && ($noticeCreatePerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "创建人的长度不能大于50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 通知标题
        if ($.isNotBlank($noticeName.val()) && ($noticeName.val().length > 500)) {
            $.toast({
                top: 5,
                msg: "通知标题的长度不能大于500个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 开始日期必须小于结束日期
        if ($.isNotBlank($startDate.val()) && $.isNotBlank($endDate.val())) {
            if (Number($startDate.val().replaceAll("-", "")) > Number($endDate.val().replaceAll("-", ""))) {
                $.toast({
                    top: 5,
                    msg: "开始日期应小于结束日期",
                    type: "warn",
                    close: 3000
                });

                return;
            }
        }

        $noticeName.val($.trim($noticeName.val()));
        $noticeCreatePerson.val($.trim($noticeCreatePerson.val()));

        // 搜索开始…
        if ($.isNotNull(noticeTable)) {
            noticeTable.myAjaxData = {
                endDate: $endDate.val(),
                startDate: $startDate.val(),
                noticeName: $noticeName.val(),
                noticeLevel: $noticeLevel.val(),
                noticeCreatePerson: $noticeCreatePerson.val()
            };

            // 刷新通知表格（分页重置）
            noticeTable.ajax.reload();
        }
    }
});